package com.zxkj.commonpatrol.adapt;

import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.response.PointInspectDeviceResponse;
import com.zxkj.commonpatrol.utils.AddViewTypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddDeciveCustomViewAdapt extends BaseQuickAdapter<PointInspectDeviceResponse.DeviceProperty, BaseViewHolder> {
    public Map<String,String> Values=new HashMap<>();
    public AddDeciveCustomViewAdapt(int layoutResId, @Nullable List<PointInspectDeviceResponse.DeviceProperty> data) {
        super(layoutResId, data);

    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable PointInspectDeviceResponse.DeviceProperty mProfileEntrity) {
        TextView tvName=baseViewHolder.itemView.findViewById(R.id.tv_addview);
        EditText editText=baseViewHolder.itemView.findViewById(R.id.ed_add_view);
        editText.setText(mProfileEntrity.getValue());
        Values.put(mProfileEntrity.getCode(),mProfileEntrity.getValue());
        final TextWatcher textWatcher =new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editText.hasFocus())
                    Values.put(mProfileEntrity.getCode(),s.toString());
            }
        };
        editText.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                editText.addTextChangedListener(textWatcher);
            }else{
                editText.removeTextChangedListener(textWatcher);
            }
        });
        tvName.setText(mProfileEntrity.getName());
        if (mProfileEntrity.getType()==0){
            editText.setFocusableInTouchMode(true);
            editText.setClickable(false);
            editText.setFocusable(true);
            Drawable drawable = getContext().getResources().getDrawable(
                    R.drawable.write_y_small);
            // 这一步必须要做，否则不会显示。
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            editText.setCompoundDrawables(null, null, drawable, null);


        }else{
            editText.setFocusableInTouchMode(false);
            editText.setFocusable(false);
            editText.setClickable(true);
            Drawable drawable = getContext().getResources().getDrawable(
                    R.drawable.ddm_ic_arrow_down);
            // 这一步必须要做，否则不会显示。
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            editText.setCompoundDrawables(null, null, drawable, null);
        }

        editText.setOnClickListener(v->{



            if (mProfileEntrity.getType()==1){
                //datatime
                AddViewTypeUtils.AddDatatime(editText, getContext(), new AddViewTypeUtils.MyBaseDataCallBack() {
                    @Override
                    public void onDataSuccessCallBack(Object o) {
                        Values.put(mProfileEntrity.getCode(),(String)o);
                    }

                    @Override
                    public void onDataFailCallBack(Object o, Object t1) {

                    }
                });

            }else if(mProfileEntrity.getType()==2){
                if (mProfileEntrity.getData()!=null){
                    if (mProfileEntrity.getData().size()>0){
                        int size4 = mProfileEntrity.getData().size();
                        String[] array4 = (String[]) mProfileEntrity.getData().toArray(new String[size4]);
                        //combox
                        AddViewTypeUtils.AddSpinner(editText, getContext(),array4, new AddViewTypeUtils.MyBaseDataCallBack() {
                            @Override
                            public void onDataSuccessCallBack(Object o) {
                                Values.put(mProfileEntrity.getCode(),(String)o);
                            }

                            @Override
                            public void onDataFailCallBack(Object o, Object t1) {

                            }
                        });
                    }
                }

            }

        });

    }
}
